package com.angel.bo.admin.service.impl;

import com.angel.bo.admin.constants.DictCodeConstant;
import com.angel.bo.admin.exception.AdminException;
import com.angel.bo.admin.mapper.UserRoleMapper;
import com.angel.bo.admin.service.RoleService;
import com.angel.bo.admin.service.UserRoleService;
import com.angel.bo.admin.service.UserService;
import com.angel.bo.beans.admin.dto.UserRoleDto;
import com.angel.bo.beans.admin.entity.Role;
import com.angel.bo.beans.admin.entity.User;
import com.angel.bo.beans.admin.entity.UserRole;
import com.angel.bo.beans.admin.vo.UserRoleVo;
import com.angel.bo.cache.annotation.DictParam;
import com.angel.bo.cache.annotation.TranslationDict;
import com.angel.bo.common.constant.ReturnCode;
import com.angel.bo.common.msg.WebReturn;
import com.angel.bo.common.service.impl.BaseServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 
 *
 * @author ${author}
 * @email haibo.mei@ge.com
 * @date 2018-02-01 14:32:20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserRoleServiceImpl extends BaseServiceImpl<UserRoleMapper,UserRole> implements UserRoleService {


    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleMapper userRoleMapper;


    /**
     * 给给用户授权角色
     *
     * @param userRoleVo
     * @return
     */
    @Override
    public WebReturn configUserRole(UserRoleVo userRoleVo) {

        if(userRoleVo.getRoleIds() == null || "".equals(userRoleVo.getRoleIds())){
            throw new AdminException(ReturnCode.JSON_DATA_ERROR);
        }
        User user = userService.selectById(userRoleVo.getUserId());
        //先删除角色
        UserRole deleteEntity = new UserRole();
        deleteEntity.setUserId(userRoleVo.getUserId());
        super.delete(deleteEntity);
        String[] roleIdStrs = userRoleVo.getRoleIds().split(",");
        for (String roleIdStr : roleIdStrs) {
            Role role = roleService.selectById(Integer.valueOf(roleIdStr));
            UserRole userRole = new UserRole();
            userRole.setUserId(userRoleVo.getUserId());
            userRole.setRoleId(Integer.valueOf(roleIdStr));
            userRole.setCreateDate(new Date());
            userRole.setState(DictCodeConstant.USER_STATE_CODE_1);
            userRole.setUserRoleName(user.getUserName() + "(" + role.getRoleName() + ")");
            super.insert(userRole);
        }

        WebReturn webReturn = new WebReturn();
        webReturn.setMsg("配置角色成功");
        return webReturn;
    }

    /**
     * 获取用户角色配置信息
     *
     * @param userRoleVo
     * @return
     */
    @Override
    public WebReturn getUserRoleList(UserRoleVo userRoleVo) {
        UserRole entity = new UserRole();
        entity.setUserId(userRoleVo.getUserId());
        List<UserRole> userRoles = super.selectList(entity);
        WebReturn webReturn = new WebReturn();
        webReturn.setData(userRoles);
        return webReturn;
    }

    /**
     * 获取用户角色实例列表
     *
     * @param userRoleVo
     * @return
     */
    @Override
    @TranslationDict({@DictParam(dictCode = "USER_GENDER_CODE",dictValueFiled = "gender",dictNameFiled = "genderName")})
    public WebReturn getUserRolePage(UserRoleVo userRoleVo) {

        WebReturn webReturn = new WebReturn();
        PageHelper.startPage(userRoleVo.getPageNum(),userRoleVo.getPageSize());
        Page<UserRoleDto> list = userRoleMapper.getUserRolePage(userRoleVo);
        PageInfo page = new PageInfo<>(list);
        webReturn.setData(page);
        return webReturn;
    }
}



